<?php
namespace app\api\controller;

use app\api\controller\Base;

/**
 * 公共信息
 */
class Room extends Base
{
    /**
     * 获取商品分类列表信息
     * @url     /Hotelrule/index
     * @http    get
     * @data    {cate_id}
     * @return  array of getGoodsTypeList item , code 200
     *
     */
    public function roomList()
    {
        if (request()->isPost()) {
            $cate_id = input('post.cate_id');
            $start_time = input('start_time');
            $end_time = input('end_time');
            if (isset($cate_id)) {
                $list = db('room')
                    ->alias('r')
                    ->join('room_cate c', 'r.two_cate = c.id')
                    ->where(['r.one_cate' => $cate_id, 'c.status' => 0, 'r.status' => 0,'r.is_activity' => 0 ])
                    ->field(['r.id'=>'id','c.title' => 'title', 'r.logo' => 'image', 'r.bed' => 'bed', 'r.old_price' => 'old_price','r.new_price'=>'new_price','r.area'=>'area','r.floor'=>'floor','r.description'=>'description','is_coupon'=>'is_coupon','is_recharge'=>'is_recharge','is_second'=>'is_second','r.order_num'=>'order_num','r.description'=>'description','r.images'=>'banner','r.room_num'=>'room_num'])
                    ->select();
                // 现在问题是先找房间价格  还是先筛选好房间
                // 按照程序逻辑来说应该是先找房间价格
                // 那么先用一个变量来保存最终结果$arr 然后使用for循环来遍历这些房间
                $arr = array();
                // 首先找出客人都哪些天要住
                $arr_day = db('time')->where(['date'=>['BETWEEN',[$start_time,$end_time]]])->select();
               
                if($cate_id == 0){
                    // 为过夜房的时候需要将选择的最后一天减去 而钟点房不需要
                    array_pop($arr_day);
                }
               
                // 获取时间的id集合
                $arr_id = array_column($arr_day, 'id');
            
                // 然后计算出要住多少天
                $num_day = count($arr_day);
          
                foreach ($list as $i => $item) {
                    // 这里面的是单个房间
                    // 先找这几天空房并且正常状态下的价格集合
                    $arr_prices = db('room_price')->where(['room_id'=>$item['id'],'time_id'=>['IN',$arr_id],'status'=>0,'num'=>['<>',0]])->select();
                    //判断这几天空房的天数与用户要订购的天数是否一致  一致的就是用户可以定的  不一致就不可以订
                    if(count($arr_prices) == $num_day ){

                        // 计算价格总和
                        $price = 0;
                        for($i = 0;$i < $num_day;$i++){
                            $price += $arr_prices[$i]['price'];
                         
                        }
                        $item['new_price'] = round($price*100/$num_day)/100;
                        $item['old_price'] = $item['old_price'];
                        $arr[] = $item;
                    }
                }
               
                // 查出来了房间的信息  那么如何找到这些房间在这段时间内都是有房间的？  而且房间对价格是一对多的关系
                if($cate_id==0){
                   $user=db('user')->where(['user_id'=>$this->user_id])->find();
                    $viplist = db('vip_cate')->where(['status'=>0])->order('level desc')->select();
                    $data['roomlist'] = $arr;
                    $data['user']=$user;
                    $data['viplist'] = $viplist;
                    return model('Res')->ctx($data);
                }else{
                    return model('Res')->ctx($arr);
                }
               
            } else {
                return model('Res')->noParam();
            }
        } else {
            return model('Res')->noPost();
        }
    }
    public function roominfo()
    {
        if(request()->isPost()){
            $id = input('post.id');
            if($id){
                $start_time = input('start_time');
                $cate_id = input('post.cate_id');
                if(!isset($start_time)){
                    $start_time = date('Y-m-d');
                }
                
                $end_time = input('end_time');
                if(!isset($end_time)){
                    if($cate_id == 0){
                        $end_time = date('Y-m-d',time()+24*3600);
                    }else{
                        $end_time = date('Y-m-d');
                    }
                }
               
                $list = db('room')->where(['id'=>$id])->find();
                $list['title'] = db('room_cate')->where(['id'=>$list['two_cate']])->field('title')->find()['title'];
                // 首先找出客人都哪些天要住
                
                $arr_day = db('time')->where(['date'=>['BETWEEN',[$start_time,$end_time]]])->select();

                if($cate_id == 0){
                    // 为过夜房的时候需要将选择的最后一天减去 而钟点房不需要
                    array_pop($arr_day);
                }

                // 获取时间的id集合
                $arr_id = array_column($arr_day, 'id');
               
                // 然后计算出要住多少天
                $num_day = count($arr_day);
                // return $num_day;
                // 找到这些价格
                $arr_prices = db('room_price')->where(['room_id'=>$id,'time_id'=>['IN',$arr_id],'status'=>0,'num'=>['<>',0]])->select();
                // if($arr_prices)
                
                $price = 0;
               
                for($i = 0;$i < $num_day;$i++){
                    $price += $arr_prices[$i]['price'];
                         
                }
                $list['new_price'] = $price;
                $list['old_price'] = $num_day*$list['old_price'];
                $list['days'] = $num_day;
               

                
                
                return model('Res')->ctx($list);
            }else{
                return model('Res')->noParam();
            }
        }else{
            return model('Res')->noPost();
        }
    }
    /**
     * 酒店评论
    */
    public function comment(){

        $page = input('page',1);
        $limit = 10;
        $offset = ($page - 1) * $limit; 
        $list = db("comment a")
            ->join('think_user b','a.user_id=b.user_id')
            ->where(['a.status'=>1])
            ->field('a.id,a.content,a.addtime,a.images,b.nickname,avatar')
            ->limit($offset, $limit)
            ->order("a.addtime desc")
            ->select();
        foreach($list as $k=>&$v){
            $v['addtime'] = date('Y-m-d',$v['addtime']);
            $v['images'] = explode(',',$v['images']);
        }
        
        return model('Res')->ctx($list);
    }
    // 活动房间
    public function activityroom(){
        $id = input('id');
        if($id){
            $activity = db('activity')->where(['id'=>$id])->find();
            $start_time = input('start_time');
            $end_time = input('end_time');
            $roomlist = db('room')
                ->alias('r')
                ->join('room_cate c','c.id = r.two_cate')
                ->where(['r.id'=>['in',$activity['param']]])
                ->field('c.title as title,r.logo as logo,r.id as id,new_price,bed,area,images,area,floor,description,room_num,order_num,one_cate')
                ->select();
            

            $arr = array();
            // 首先找出客人都哪些天要住
            $arr_day = db('time')->where(['date'=>['BETWEEN',[$start_time,$end_time]]])->select();
            
            
           
            // 获取时间的id集合
            $arr_id = array_column($arr_day, 'id');
        
            // 然后计算出要住多少天
            $num_day = count($arr_day);

            foreach ($roomlist as $i => $item) {
                $ite_time = $arr_day;
                if($item["one_cate"] == 0){
                    // 为过夜房的时候需要将选择的最后一天减去 而钟点房不需要
                    array_pop($ite_time);
                }
                $time_id = array_column($ite_time, 'id');
                // 这里面的是单个房间
                // 先找这几天空房并且正常状态下的价格集合
                $arr_prices = db('room_price')->where(['room_id'=>$item['id'],'time_id'=>['IN',$time_id],'status'=>0,'num'=>['<>',0]])->select();

                //判断这几天空房的天数与用户要订购的天数是否一致  一致的就是用户可以定的  不一致就不可以订
                if(count($arr_prices) == count($ite_time) ){

                    // 计算价格总和
                    $price = 0;
                    for($i = 0;$i < $num_day;$i++){
                        $price += $arr_prices[$i]['price'];
                     
                    }
                    $item['new_price'] = round($price*100/$num_day)/100;
                    $item['old_price'] = $item['old_price'];
                    $arr[] = $item;
                }
            }
            $data['room'] = $arr;
            $activity['start_time'] = date('m月d日',$activity['start_time']);
            $activity['end_time'] = date('m月d日',$activity['end_time']);
            $data['activity'] = $activity;
            return model('Res')->ctx($data);
        }else{
            return model('Res')->noParam();
        }
    }
}
